package com.bsh.repository;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.bsh.mybatis.IRepository.MessageMapper;
import com.bsh.mybatis.model.Message;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

/**
 * 系统消息
 * @author gsh
 *
 */
@Repository
public class DaoMessage {
	@Resource
	SqlSessionTemplate session;

	@Resource
	JdbcTemplate jdbcTemplate;
	@Resource
	NamedParameterJdbcTemplate namedJdbcTemplate;

	MessageMapper getMapper() {
		return session.getMapper(MessageMapper.class);
	}

	public int deleteByPrimaryKey(Integer id) {
		int res = getMapper().deleteByPrimaryKey(id);
		return res;
	}

	public int insert(Message record) {
		int res = getMapper().insert(record);
		return record.getId();
	}

	public int insertSelective(Message record) {
		int res = getMapper().insertSelective(record);
		return record.getId();
	}

	public Message selectByPrimaryKey(Integer id) {
		Message row = getMapper().selectByPrimaryKey(id);
		return row;
	}

	public int updateByPrimaryKeySelective(Message record) {
		int res = getMapper().updateByPrimaryKeySelective(record);
		return res;
	}

	public int updateByPrimaryKey(Message record) {
		int res = getMapper().updateByPrimaryKeyWithBLOBs(record);
		return res;
	}
	
	/**
	 * 消息列表分页查询
	 */
	public List<Map<String, Object>> getPage(Pager pager, String where) {
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append("SELECT a.id,a.msg_type,a.title,a.create_time,status FROM message a ");
		sb.append("where 1=1 ");
		if (!StringUtils.isNullOrEmpty(where)) {
			sb.append(where);
		}
		Integer count = namedJdbcTemplate.queryForInt("select count(0) from (" + sb.toString() + ")v", ps);
		sb.append(" order by a.id desc limit :start,:size");
		ps.addValue("start", pager.getStart());
		ps.addValue("size", pager.getPageSize());
		List<Map<String, Object>> list = namedJdbcTemplate.queryForList(sb.toString(), ps);
		pager.setRowCount(count);
		return list;
	}
	
	
	/**
	 * 消息列表分页查询 api
	 */
	public List<Map<String, Object>> getMessageListPage(Pager pager, String where) {
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append(" SELECT a.id,a.msg_type,a.title,a.create_time FROM message a ");
		sb.append(" WHERE status=1");
		if (!StringUtils.isNullOrEmpty(where)) {
			sb.append(where);
		}
		Integer count = namedJdbcTemplate.queryForInt("select count(0) from (" + sb.toString() + ")v", ps);
		sb.append(" order by a.create_time desc limit :start,:size");
		ps.addValue("start", pager.getStart());
		ps.addValue("size", pager.getPageSize());
		List<Map<String, Object>> list = namedJdbcTemplate.queryForList(sb.toString(), ps);
		pager.setRowCount(count);
		return list;
	}
	
	
}
